<?php
require_plugins('simple_html_dom.php');
class constellation__gather__sina_apiBase extends apiBase {
    function __construct() {
        //http://vip.astro.sina.com.cn/astro/view/taurus/day/20120411
        $this->baseTemplate="http://vip.astro.sina.com.cn/astro/view/%s/day/%s";
        $this->contellationId=array(2=>array('key'=>'taurus','name'=>'金牛'),3=>array('key'=>'lion','name'=>'狮子'));
        $this->textKey=array('综合运势'=>'zhzs','爱情运势'=>'aqys','工作状况'=>'gzzk','理财投资'=>'lctz','健康指数'=>'jkzs','商谈指数'=>'stzs','幸运颜色'=>'xyys','幸运数字'=>'xysz','速配星座'=>'spxz');
        $this->colorId=array('银色'=>2);
        
        
    }
    
    /**
      *  获取某个星座某天的运势数据 
      */
    function getTread($cid,$nyr){
        $url=sprintf($this->baseTemplate,$this->contellationId[$cid]['key'],$nyr);
        $html = file_get_html($url);
        $rst=array();
        foreach($html->find('.lotstars') as $lotstars){
            $img_index=4;
            foreach($lotstars->find('.tab') as $row){
                $k=trim($row->find('h4',0)->plaintext);
                $k=str_replace('&nbsp;','',$k);
                
                if(!$k) continue;
                if($img_index-->0){
                    $v=count($row->find('img'));    
                }
                else{
                    $v=type_correct(trim($row->find('p',0)->plaintext));
                }
                if(isset($this->textKey[$k])){
                    $k=$this->textKey[$k];
                }
                else{
                    $k=utf8_encode($k);
                }
                if($k=='xyys'){
                    $v=isset($this->colorId[$v])?$this->colorId[$v]:$v;
                }
                elseif($k=='spxz'){
                    foreach($this->contellationId as $key=>$value){
                        if(mb_stripos($v,$value['name'],NULL,'utf8')===0){
                            $v=$key;
                            break;
                        }
                    }
                }
                $rst[$k]=$v;
            }
        }
        $rst['content']=$html->find('.lotconts',0)->plaintext;
        return $rst;
    }
    
}
?>